﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Dzzzr;
using Telerik.Web.UI;

namespace NWS.Games
{
  public partial class Assignment : System.Web.UI.Page
  {
    private const string GameID = "GameID";
    protected void Page_Load(object sender, EventArgs e)
    {
			if(!IsPostBack)
				DataBind();
    }

		protected void AssignmentTreeView_NodeDrop(object sender, Telerik.Web.UI.RadTreeNodeDragDropEventArgs e)
		{
			int sourceID = Convert.ToInt32(e.SourceDragNode.Value);
			int gameID = Convert.ToInt32(Request[GameID]);
			if (e.DestDragNode != null)
			{
				int destID = Convert.ToInt32(e.DestDragNode.Value);
				GameEntityController.SetEntity(gameID, sourceID, destID, (GameEntityController.DropPosition) e.DropPosition);
			}
			else
			{
				GameEntityInfo entity = new GameEntityInfo();
				entity.GameID = gameID;
				entity.EntityID = sourceID;
				GameEntityController.DeleteAssignment(entity);
			}
			DataBind();
		}

  	private void DataBind()
  	{
  		AssignmentTreeView.DataBind();
  		AssignmentTreeView.ExpandAllNodes();
  		if (AssignmentTreeView.Nodes.Count > 0)
  			AssignmentTreeView.Nodes[0].Selected = true;
  		RemainedGrid.DataBind();
  	}

  	protected void remainedGrid_ItemCommand(object source, GridCommandEventArgs e)
		{
			int destID = -1;
			if (!String.IsNullOrEmpty(AssignmentTreeView.SelectedValue))
				destID = Convert.ToInt32(AssignmentTreeView.SelectedValue);
			int sourceID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"]);
			int gameID = Convert.ToInt32(Request[GameID]);

			GameEntityController.DropPosition dropPosition = GameEntityController.DropPosition.Into;
  		switch (e.CommandName)
  		{
				case "AddChild":
					dropPosition = GameEntityController.DropPosition.Into;
  				break;
				case "AddAfter":
					dropPosition = GameEntityController.DropPosition.After;
					break;
				case "AddBefore":
					dropPosition = GameEntityController.DropPosition.Before;
					break;
			}

			GameEntityController.SetEntity(gameID, sourceID, destID, dropPosition);
			DataBind();
		}

		protected void TreeToolbar_ButtonClick(object sender, RadToolBarEventArgs e)
		{
			if(((RadToolBarButton)e.Item).CommandName == "Remove")
			{
				if (AssignmentTreeView.SelectedValue != null)
				{
					GameEntityInfo entity = new GameEntityInfo();
					entity.GameID = Convert.ToInt32(Request[GameID]);
					entity.EntityID = Convert.ToInt32(AssignmentTreeView.SelectedValue);
					GameEntityController.DeleteAssignment(entity);
				}
				DataBind();
			}
		}

		protected void AssignmentTreeView_NodeDataBound(object sender, RadTreeNodeEventArgs e)
		{
			GameEntityInfo entity = (GameEntityInfo) e.Node.DataItem;
			switch (entity.EntityTypeID)
			{
				case 1:
					e.Node.ImageUrl = "~/Images/person.png";
					break;
				case 2:
					e.Node.ImageUrl = "~/Images/car.png";
					break;
				default:
					e.Node.ImageUrl = "~/Images/question.png";
					break;
			}
		}
  }
}
